package com.example.dwhne.leonweather;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class MyPageAdapter extends PagerAdapter {
    private List<View>viewList;
    private List<String>titleList;
    public MyPageAdapter(List<View>viewList, List<String>titleList){
        this.viewList=viewList;
        this.titleList=titleList;
    }
    /**
     * 返回页卡的数量
     * @return
     */

    @Override
    public int getCount() {
        return viewList.size();
    }

    /**
     * View是否来自于对象
     * @param view
     * @param o
     * @return
     */
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view==o;
    }

    /**
     * 实例化一个页卡
     * @param container
     * @param position
     * @return
     */
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //return super.instantiateItem(container, position);
        container.addView(viewList.get(position));
        return viewList.get(position);
    }

    /**
     * 销毁一个页卡
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //super.destroyItem(container, position, object);
        container.removeView(viewList.get(position));
    }

    /**
     * 设置页卡标题
     * @param position
     * @return
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}
